<template>
  <el-form :model="postform" status-icon :rules="rules" ref="postform" label-width="100px" >
    <el-form-item label="收款人账号" prop="tradingUserName">
      <el-input v-model="postform.tradingUserName" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="转账金额" prop="transferAmount">
      <el-input type="number" v-model.number="postform.transferAmount" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交转账</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { Transfer } from '@/api/user'

export default {
  name: 'transferEdit',
  data() {
    return {
      postform: {
        tradingUserName: '',
        transferAmount: 0
      },
      rules: {
        tradingUserName: [
          { required: true, message: '请输入收款人账号', trigger: 'blur' },
          { min: 3, max: 32, message: '长度在 3 到 32 个字符', trigger: 'blur' }
        ],
        transferAmount: [
          { required: true, message: '请输入转账金额', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: '请输入大于0的整数', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      var _this = this
      this.$refs['postform'].validate(valid => {
        if (valid) {
          _this.loading = true
          Transfer(_this.postform)
            .then(result => {
              _this.$message.success('转账成功')
              _this.$emit('transferSuccessed', result)
              _this.loading = false
            })
            .catch(response => {
              _this.loading = false
            })
        }
      })
    },
    resetForm() {
      this.$refs['postform'].resetFields()
    }
  }
}
</script>
